{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PCA方法\n",
    "- 函数原型: sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False)\n",
    "- 官方文档: https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html?highlight=pca#sklearn.decomposition.PCA\n",
    "- 相关说明: https://www.jianshu.com/p/8642d5ea5389"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.41724458  0.90879423]\n",
      " [-0.90879423  0.41724458]]\n",
      "\n",
      "[[1.16076484 0.        ]]\n",
      "[[-0.96095731 -0.32068398]]\n",
      "\n",
      "[0.97468327 0.02531673]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Dimensionality Reduced')"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAAE/CAYAAAB8VnbnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAgAElEQVR4nO3dfbQkd13n8fcnxIgTgwEzMUCYGaI8GIEFHBBUFEjQgEDQ4yrsRXn0HjkLK+sDgqOID1kREWRXF88VEJCrMRseRA7PIHjYXYKTEAwhoEFnkkhIBlweB4kh3/2j6iady32a23Wr+uH9OmdOd1d1V32r+/ZvPv2rX1WlqpAkSdLOOm7oAiRJkuaBoUuSJKkHhi5JkqQeGLokSZJ6YOiSJEnqgaFLkiSpB4auHZLkj5P82tB1rEiyJ8mXktxmh9fz5CQfGHn8pSRn7OQ6+5SkknxHz+s8lOTsPtcprce27ebHE9u22U5NruOHLmAaJTkEfBtwI/A14GPAa4GlqroJoKp+drAC11B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LrBq6hGRe34nSaqxlMpCTfQNPYLVfVG4auZx3fBzy2bXRuC9wuyeuq6okD17XaNcA1VbXyS/1CmtA1uM2+T+vY6t/ydUnuWFXXtrteNhw+sYXv9vHAj7HBeRGr6lPt7fVJ3kiz+2NLwWOr70WSPwHWOmhj29/xLtq1cbZ9DVvZlh1t0zZrg0ZDWFW9Ncn/THJKVY19bcItvJd9tOePBC6pquvWqG/Htn3EVr6/23ofhty9+CaaSweR5O40A9Em6sK9VXVZVZ1aVfuqah/Nm3z/IQLXZpKcA/wy8NiqOjp0Pav8HXC3JHdtf8E8HnjzwDWtqR0380rgiqp6ydD1rKeqnldVp7d/l48H3juBgYv2u3J1knu0k84CPjZgSeN6M/D4JN+Y5K40v7A/tM7zntTefxKwXs/ZVp0NfLyqrllrZpITk5y0cp+md/6jaz33WK0ar/Oj6yx3R77jW2nXdmDbt7ItbwZ+Oo0H0Vx3+Nox1nmzrbRBSU5rn0eSB9L8X/7ZDta9lfdyx7Z9xLq9uju17ats5fu7vb/5zUba79Q/mpD1OpoP9BLg4UPVcgw1H2Jyj168kmb/8qXtvz8euqZV9T2K5iicT9LsNhi8pnXq/H6aLuK/H3kvHzV0XZvU/FAm++jF+wIH2/f0TcDth65pCzX/KM2PrK8C1wHvGJl3oP07/gTwyJHpr6A90hH4VuA9NNejfQ9whzHreTXws6um3Ql4a3v/DJqjpz4CXN7ldwz4M+Cy9vN7M3DH1etvH3f+HV+vXdvpbV9rW4CfXfkMaHYt/VE7/zJGjnDtYN1rtkGr1v/Mdls/QnOAwfd2tO4138u+tr1d/i6aEPUtI9N2bNtpwt21wL+33/mnrff97eJv3jPSS5Ik9cAz0kuSJPXA0CVJktQDQ5ckSVIPDF2SJEk9MHRJkiT1wNAlSZLUA0OXJElSDwxdkiRJPfj/3hkn/LVswzsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x360 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.decomposition import PCA\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "np.random.seed(0)\n",
    "\n",
    "X = np.linspace(-5, 5, 20)\n",
    "X = X.reshape((X.shape[0], 1))\n",
    "X = np.concatenate((X, 2*X), axis=1)\n",
    "X = X + np.random.normal(loc=0, scale=1, size=X.shape)\n",
    "\n",
    "\n",
    "# 计算关联系数矩阵\n",
    "# np.cov中的bias=False表示除以N-1而不是N\n",
    "R = np.cov(X.T, bias=False)\n",
    "# print(R)\n",
    "eigenvalues, eigenvectors = np.linalg.eig(R)\n",
    "order = eigenvalues.argsort()[::-1]\n",
    "eigenvalues_order = eigenvalues[order]\n",
    "eigenvectors = eigenvectors[order]\n",
    "eigenvalues[1] = -eigenvalues[1]\n",
    "\n",
    "# PCA训练器\n",
    "pca = PCA(n_components=2, copy=True, whiten=False)\n",
    "# 训练并降维\n",
    "pca.fit(X)\n",
    "x0 = [pca.components_[:, 0]]\n",
    "newX = pca.transform(x0)\n",
    "\n",
    "print(pca.components_)\n",
    "print()\n",
    "print(np.matmul(x0, pca.components_*eigenvalues))\n",
    "print(newX)\n",
    "# 将降维后数据逆变换回原数据\n",
    "pca.inverse_transform(newX)\n",
    "# 各个特征的方差百分比\n",
    "print()\n",
    "print(pca.explained_variance_ratio_)\n",
    "#print(pca.explained_variance_ratio_) # 即特征值百分比\n",
    "#print(pca.explained_variance_) # 即特征值\n",
    "\n",
    "newX = pca.transform(X)\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.scatter(X[:,0], X[:, 1], color='b')\n",
    "plt.xlim(xmin=-6, xmax=6)\n",
    "plt.ylim(ymin=-8, ymax=8)\n",
    "plt.title('Dimensionality Unreduced')\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.scatter(newX[:,0], newX[:, 1], color='r')\n",
    "plt.xlim(xmin=-10, xmax=10)\n",
    "plt.ylim(ymin=-7, ymax=7)\n",
    "plt.title('Dimensionality Reduced')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
